package com.ruoyi.web.controller.blog;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.vo.BlogContentVo;
import com.ruoyi.blog.domain.bo.BlogContentBo;
import com.ruoyi.blog.service.IBlogContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 博客内容Controller
 *
 * @author zhutianyu
 * @date 2021-08-19
 */
@Validated
@Api(value = "博客内容控制器", tags = {"博客内容管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/blog/content")
public class BlogContentController extends BaseController {

    private final IBlogContentService iBlogContentService;

    /**
     * 查询博客内容列表
     */
    @ApiOperation("查询博客内容列表")
    @PreAuthorize("@ss.hasPermi('blog:content:list')")
    @GetMapping("/list")
    public TableDataInfo<BlogContentVo> list(@Validated BlogContentBo bo) {
        return iBlogContentService.queryPageList(bo);
    }

    /**
     * 导出博客内容列表
     */
    @ApiOperation("导出博客内容列表")
    @PreAuthorize("@ss.hasPermi('blog:content:export')")
    @Log(title = "博客内容", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<BlogContentVo> export(@Validated BlogContentBo bo) {
        List<BlogContentVo> list = iBlogContentService.queryList(bo);
        ExcelUtil<BlogContentVo> util = new ExcelUtil<BlogContentVo>(BlogContentVo.class);
        return util.exportExcel(list, "博客内容");
    }

    /**
     * 获取博客内容详细信息
     */
    @ApiOperation("获取博客内容详细信息")
    @PreAuthorize("@ss.hasPermi('blog:content:query')")
    @GetMapping("/{id}")
    public AjaxResult<BlogContentVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return AjaxResult.success(iBlogContentService.queryById(id));
    }

    /**
     * 新增博客内容
     */
    @ApiOperation("新增博客内容")
    @PreAuthorize("@ss.hasPermi('blog:content:add')")
    @Log(title = "博客内容", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogContentBo bo) {
        return toAjax(iBlogContentService.insertByBo(bo));
    }

    /**
     * 修改博客内容
     */
    @ApiOperation("修改博客内容")
    @PreAuthorize("@ss.hasPermi('blog:content:edit')")
    @Log(title = "博客内容", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogContentBo bo) {
        return toAjax(iBlogContentService.updateByBo(bo));
    }

    /**
     * 删除博客内容
     */
    @ApiOperation("删除博客内容")
    @PreAuthorize("@ss.hasPermi('blog:content:remove')")
    @Log(title = "博客内容" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iBlogContentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
